package com.craftworks.tiles;

import cn.hutool.core.util.RandomUtil;
import com.craftworks.tiles.impl.*;
import com.formdev.flatlaf.themes.FlatMacDarkLaf;

import javax.swing.*;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.concurrent.atomic.AtomicInteger;

public class Demo
{
    public static void main(String[] args)
    {
        FlatMacDarkLaf.setup();

        SwingUtilities.invokeLater(() -> {
            JFrame frame = new JFrame();
            frame.setTitle("X-Tiles");
            frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

            AbstractTile tile1 = new DateTile();
            tile1.setProperty("date", LocalDate.now());

            AbstractTile tile2 = new ClockTile();
            Timer timer1 = new Timer(100, e -> tile2.setProperty("time", LocalDateTime.now()));

            AbstractTile tile3 = TextTile.large();
            tile3.setProperty("title", "黄鹤楼  【唐】崔颢");
            tile3.setProperty("text", """
                                      昔人已乘黄鹤去，此地空余黄鹤楼。
                                      黄鹤一去不复返，白云千载空悠悠。
                                      晴川历历汉阳树，芳草凄凄鹦鹉洲。
                                      日暮乡关何处是，烟波江上使人愁。
                                      """);

            AbstractTile tile4 = new NetTrafficTile();
            tile4.setProperty("port", "tcp://224.0.0.198:6789");
            tile4.setProperty("in.bitrate", 23904392);
            tile4.setProperty("out.bitrate", 133);
            Timer timer2 = new Timer(200, e -> {
                tile4.setProperty("in.bitrate", RandomUtil.randomInt(10, 1000000000));
                tile4.setProperty("out.bitrate", RandomUtil.randomInt(10, 1000000000));
            });

            AbstractTile tile5 = DigitTile.compact();
            tile5.setProperty("digits", 12);
            tile5.setProperty("unit", "°C");
            tile5.setProperty("title", "Temperature");

            AbstractTile tile6 = DigitTile.wide();
            tile6.setProperty("digits", "123");
            tile6.setProperty("digits.color", "#0073e6");
            tile6.setProperty("unit", "€");
            tile6.setProperty("unit.color", "#eeff00");
            tile6.setProperty("desc", "incoming today");

            AtomicInteger ring1 = new AtomicInteger(0);
            AtomicInteger ring2 = new AtomicInteger(0);
            AtomicInteger ring3 = new AtomicInteger(0);
            AbstractTile tile7 = new RingTile();
            Timer timer3 = new Timer(50, e -> {
                tile7.setProperty("ring1.value", ring1.addAndGet(5) / 100.0);
                tile7.setProperty("ring2.value", ring2.addAndGet(3) / 100.0);
                tile7.setProperty("ring3.value", ring3.addAndGet(2) / 100.0);

                if (ring1.get() > 100)
                    ring1.set(0);
                if (ring2.get() > 100)
                    ring2.set(0);
                if (ring3.get() > 100)
                    ring3.set(0);
            });

            AbstractTile tile8 = new TrackTile();
            tile8.setProperty("bound.low", 0);
            tile8.setProperty("bound.high", 20);
            tile8.setProperty("refresh.interval", 0.1);
            Timer timer4 = new Timer(100, e -> tile8.setProperty("plot.value", RandomUtil.randomInt(0, 20)));

            AbstractTile tile9 = new GradientRingTile();
            tile9.setProperty("ring.value", 1);

            TileDashboard board = new TileDashboard();
            board.add(tile1);
            board.add(tile2);
            board.add(tile3);
            board.add(tile4);
            board.add(tile5);
            board.add(tile6);
            board.add(tile7);
            board.add(tile8);
            board.add(tile9);

            frame.getContentPane().add(board.getView());

            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);

            timer1.start();
            timer2.start();
            timer3.start();
            timer4.start();
        });
    }
}
